from flask import Blueprint, jsonify, request
from App.models.db_models import EmailVerify, User, UserInformation
from App.models._models import add,updata,close
from App.models.practice_models import select
from App.models.user_models import Login_State, Mail, Register_State, select_code
import re
from App.models import getSingleMa



user = Blueprint('user', __name__, url_prefix="/user")


@user.route('/login', methods=['POST'])
def user_login():
    eMail = request.json.get('email')
    passWord = request.json.get('password')

    if eMail:
        email_exists = User.query.filter(User.email == eMail).first()
        if email_exists:
            password_one = User.query.filter(
                User.email == eMail).first()
            if passWord in password_one.password:
                return jsonify({"code": 207, "msg": "密码正确，登录成功！","username":password_one.username})
            else:
                return Login_State(205)
        else:
            return Login_State(203)
    else:
        return Login_State(208)


@user.route('/register', methods=['POST'])
def user_register():
    eMail = request.json.get('email')
    coDe = request.json.get('evc')
    userName = request.json.get('username')
    passWord = request.json.get('password')
    code_list=EmailVerify.query.filter(EmailVerify.email==eMail).all()
    print(code_list)
    code_list=select_code(code_list)
    print(code_list)
    print(coDe)
    if coDe not in code_list:
            return Register_State(208)
    if userName and passWord:

        # if re.match("^(?:(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])).*$", userName) == None:
        #     return Register_State(202)
        if re.match("^(?:(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])).*$", passWord) == None:
            return Register_State(203)
        else:
            add(User(email=eMail,username=userName, password=passWord))
            add(UserInformation(email=eMail,username=userName))
            return Register_State(207)

    else:
        return Register_State(201)







@user.route('/sendmail',methods=['POST'])
def user_sendmail():
    eMail = request.json.get('email')
    code =getSingleMa()
    p1=Mail(eMail,code)
    add(EmailVerify(email=eMail,code=code))
    p1.SendMail()
    return "success"



@user.route('/information',methods=['POST'])
def user_information():
    eMail = request.json.get('email')
    information=UserInformation.query.filter(UserInformation.email==eMail).first()
    print(information.username)
    print(information.age)
    print(information.city)
    return jsonify({
            "username":information.username,
            "email":information.email,
            "age" : information.age,
            "playertype": information.playertype,
            "school": information.school,
            "city": information.city,
            "schoolCity": information.schoolCity,
            "schoolcode": information.schoolcode,
            "gender": information.gender,
    })


@user.route('/information/change',methods=['POST'])
def user_information_change():
    q=request.json.get('NewUserInformation')

    updata(UserInformation,UserInformation.email==q.get('email'),{
        UserInformation.username: q.get('username'),
        UserInformation.age: q.get('age'),
        UserInformation.playertype:q.get('playertype'),
        UserInformation.school: q.get('school'),
        UserInformation.city: q.get('city'),
        UserInformation.schoolCity:q.get('schoolCity'),
        UserInformation.schoolcode: q.get('schoolcode'),
        UserInformation.gender:q.get('gender'),
    })
    updata(User,User.email==q.get('email'),{
        User.username:q.get('username')
    })
    return "success"




